require.config({
    baseUrl: "static/js",
    paths: {
        "org/cometd": "jlib/cometd/3.0.9/cometd-all.min",
        "org/cometd/AckExtension": "jlib/cometd/3.0.9/cometd-all.min",
        "org/cometd/TimeStampExtension": "jlib/cometd/3.0.9/cometd-all.min",
        "org/cometd/TimeSyncExtension": "jlib/cometd/3.0.9/cometd-all.min",
        "org/cometd/ReloadExtension": "jlib/cometd/3.0.9/cometd-all.min",
        "jquery": 'jlib/jquery/1.11.3/jquery.min',
        "jquery.cookie": 'jlib/jqcookie/jquery.cookie.min',
        "jquery.cometd": 'jlib/cometd/3.0.9/jquery.cometd-all.min',
        "jquery.cometd-timesync": 'jlib/cometd/3.0.9/jquery.cometd-all.min',
        "jquery.cometd-timestamp": 'jlib/cometd/3.0.9/jquery.cometd-all.min',
        "jquery.cometd-reload": 'jlib/cometd/3.0.9/jquery.cometd-all.min',
        "jquery.cometd-ack": 'jlib/cometd/3.0.9/jquery.cometd-all.min'
    },

    shim: {
    	"jquery.cookie":{
    		deps: ['jquery']
    	}
    }
});


this.mo = this.mo || {};
mo.cometd = {};

(function() {
 	function bind($, cometd) {

 		function printf(msg){
 			console && console.log(msg);
 		}

		var _connected = false;
        var _disconnecting;

 		cometd.addListener('/meta/handshake', _metaHandshake);
        cometd.addListener('/meta/connect', _metaConnect);

 		function _metaHandshake(message) {
            if (message.successful) {
                _connectionInitialized();
            }
        }

       function _connectionInitialized() {
            // first time connection for this client, so subscribe tell everybody.
            $.cometd.batch(function() {
                _subscribe();
            });
        }

        function _subscribe() {
            //_chatSubscription = $.cometd.subscribe('/chat/demo', _self.receive);
            //_membersSubscription = $.cometd.subscribe('/members/demo', _self.members);

            cometd.subscribe('/demo', function(message){
            	printf("========");
                $("body").append("receive:"+message.data+"<br/>");
            	printf("receive:"+message.data);

            });
        }

        function _metaConnect(message) {
            if (_disconnecting) {
                _connected = false;
                _connectionClosed();
            } else {
                var wasConnected = _connected;
                _connected = message.successful === true;
                if (!wasConnected && _connected) {
                    _connectionEstablished();
                } else if (wasConnected && !_connected) {
                    _connectionBroken();
                }
            }
        }

        function _connectionClosed() {
           printf("---> Connection to Server Closed");
     
        }


        function _connectionBroken() {
        	printf("---> Connection to Server Broken");
     
        }

        function _connectionEstablished() {
            printf("---> Connection to Server Opened");
          
          	for (var i = 0; i < 10; i++) {
				cometd.publish("/demo", "test cometd message "+i);
			}


           
   
        }


        var emptyFn = function(){};


 		mo.cometd = {
 			start:function(){
 				var cometd_url = "http://localhost:8080/cometd/cometd";
		        cometd.configure({
		            url: cometd_url,
		            logLevel: "debug"
		        });

		        cometd.handshake();
 			}

 		}

 		return mo.cometd;

    }

    if (typeof define === 'function' && define.amd) {
        define('mo.cometd',["jquery", 'jquery.cometd','jquery.cookie',"jquery.cometd-timestamp", "jquery.cometd-timesync", "jquery.cometd-ack", "jquery.cometd-reload"], bind);
    } else {
        bind(jQuery, jQuery.cometd);
    }

})()




require(["jquery","mo.cometd"],function($,mocometd) {
	mocometd.start();
})
